package com.yungu.swift.api.route.v2.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.utils.PayXmlUtils;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.param.ApiOrderUuidParam;
import com.yungu.swift.order.service.PayService;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.yungu.swift.api.base.ApiErrorEnum.PARAM_ERROR;

/**
 * 通用支付回调 API
 *
 * @author : cuixiuyin
 * @date : 2019/11/21
 */
@Slf4j
@RestController
public class CommonPayApi {

    @Reference
    private PayService payService;

    @PostMapping(value = "/api/v2/common/checkPay")
    @ApiOperation(value = "通用预支付确认接口")
    public ResponseData<Boolean> checkPay(@RequestBody ApiOrderUuidParam orderUuidParam) {
        if (ParamUtil.isIllegal(orderUuidParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return payService.checkPay(orderUuidParam.getOrderUuid());
    }

    @PostMapping(value = "/api/v2/common/pay/callback")
    @ApiOperation(value = "通用主动执行支付回调")
    public ResponseData<String> invokeCallback(@RequestBody ApiOrderUuidParam orderUuidParam) {
        if (ParamUtil.isIllegal(orderUuidParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return payService.invokeCallback(orderUuidParam.getOrderUuid(), orderUuidParam.getUserUuid());
    }

    @PostMapping(value = {"/api/v2/common/order/removePayCache"})
    @ApiOperation(value = "清除服务器支付缓存")
    public ResponseData<Void> removePayCache(@RequestBody ApiOrderUuidParam orderUuidParam) {
        if (ParamUtil.isIllegal(orderUuidParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return payService.removePayCache(orderUuidParam.getOrderUuid());
    }

    @RequestMapping(value = "/v2/pay/wx/callback", method = {RequestMethod.POST, RequestMethod.GET})
    public String tenPayCallback(HttpServletRequest request) {
        ResponseData<String> responseData = payService.tradeCallback(OrderConstant.PAY_TYPE_TENPAY, PayXmlUtils.xmlToMap(request));
        if (responseData.isSuccess()) {
            return "<xml><return_code>SUCCESS</return_code></xml>";
        }
        log.info(responseData.getMsg() + "--第三方回调失败，流程结束");
        return "<xml><return_code>FAIL</return_code></xml>";
    }

    @RequestMapping(value = "/v2/pay/alipay/callback", method = {RequestMethod.POST, RequestMethod.GET})
    public String aliPayCallback(HttpServletRequest request) {
        ResponseData<String> responseData = payService.tradeCallback(OrderConstant.PAY_TYPE_ALIPAY,
                PayXmlUtils.build(OrderConstant.PAY_TYPE_ALIPAY, request));
        if (responseData.isSuccess()) {
            return "success";
        }
        log.info(responseData.getMsg() + "--第三方回调失败，流程结束");
        return "fail";
    }
}
